package com.chanzany.common.result;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ChanZany
 * @Date 2021/4/11 16:13
 * @Version 1.0
 */
@Accessors(chain = true)
@Data
public class R {
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    /**
     * @Description 构造函数私有化
     * @Author chanzany
     * @Param []
     * @Return
     * @Date 2021-04-11  16:04:06
     */
    private R() {

    }

    /**
     * @Description 返回成功结果 static修饰全局只会产生一个
     * @Author chanzany
     * @Param []
     * @Return com.chanzany.srb.common.result.R
     * @Date 2021-04-11  16:04:49
     */
    public static R ok() {
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode()).setMessage(ResponseEnum.SUCCESS.getMessage());
        return r;
    }

    /**
     * @Description 返回失败结果 static修饰全局只会产生一个
     * @Author chanzany
     * @Param []
     * @Return com.chanzany.srb.common.result.R
     * @Date 2021-04-11  16:04:37
     */
    public static R error() {
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode()).setMessage(ResponseEnum.ERROR.getMessage());
        return r;
    }

    /**
     * @Description 设置特定的结果
     * @Author chanzany
     * @Param [responseEnum]
     * @Return com.chanzany.srb.common.result.R
     * @Date 2021-04-11  16:04:40
     */
    public static R setResult(ResponseEnum responseEnum) {
        return new R().setCode(responseEnum.getCode()).setMessage(responseEnum.getMessage());
    }

    /**
     * @Description 为当前结果设置data
     * @Author chanzany
     * @Param  [key, value]
     * @Return  com.chanzany.srb.common.result.R
     * @Date  2021-04-11  16:04:24
     */
    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }
    public R data(Map<String,Object> map) {
        this.data = map;
        return this;
    }


    /**
     * @Description 设置特定的消息
     * @Author chanzany
     * @Param  [message]
     * @Return  com.chanzany.srb.common.result.R
     * @Date  2021-04-11  16:04:18
     */
    public  R message(String message){
        this.setMessage(message);
        return this;
    }

    /**
     * @Description 设置特定的响应码
     * @Author chanzany
     * @Param  [code]
     * @Return  com.chanzany.common.result.R
     * @Date  2021-04-11  16:04:26
     */
    public R code(Integer code){
        this.setCode(code);
        return this;
    }


}
